package com.cc.openeye.dynamic.kuikly.recommend.items.loadMore

import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.Scale
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.directives.vbind
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.views.ActivityIndicator
import com.tencent.kuikly.core.views.FooterRefresh
import com.tencent.kuikly.core.views.FooterRefreshEndState
import com.tencent.kuikly.core.views.FooterRefreshState
import com.tencent.kuikly.core.views.Text

internal class InternalLoadMoreItemView :
    ComposeView<LoadMoreItemViewAttr, LoadMoreItemViewEvent>() {

    private var footerRefreshState by observable(
        FooterRefreshState.IDLE
    )

    override fun createEvent(): LoadMoreItemViewEvent {
        return LoadMoreItemViewEvent()
    }

    override fun createAttr(): LoadMoreItemViewAttr {
        return LoadMoreItemViewAttr()
    }

    fun havaMoreData() {
        footerRefreshState = FooterRefreshState.IDLE
    }

    fun notMoreData() {
        footerRefreshState = FooterRefreshState.NONE_MORE_DATA
    }

    fun loadError() {
        footerRefreshState = FooterRefreshState.FAILURE
    }

    override fun body(): ViewBuilder {
        val ctx = this
        return {
            FooterRefresh {
                val footerRefreshView = this
                attr {
                    height(40f)
                    allCenter()
                    flexDirectionRow()
                    ctx.footerRefreshState.also {
                        when (it) {
                            FooterRefreshState.IDLE -> {
                                footerRefreshView.endRefresh(FooterRefreshEndState.SUCCESS)
                            }

                            FooterRefreshState.REFRESHING -> {}
                            FooterRefreshState.NONE_MORE_DATA -> {
                                footerRefreshView.endRefresh(FooterRefreshEndState.NONE_MORE_DATA)
                            }

                            FooterRefreshState.FAILURE -> {
                                footerRefreshView.endRefresh(FooterRefreshEndState.FAILURE)
                            }
                        }
                    }
                }

                event {
                    refreshStateDidChange {
                        ctx.footerRefreshState = it
                        if (it == FooterRefreshState.REFRESHING) {
                            ctx.event.onLoadMore?.invoke()
                        }
                    }
                    click {
                        if (ctx.footerRefreshState == FooterRefreshState.FAILURE) {
                            footerRefreshView.beginRefresh()
                        }
                    }
                }

                vbind({ ctx.footerRefreshState }) {
                    if (ctx.footerRefreshState == FooterRefreshState.REFRESHING) {
                        ActivityIndicator {
                            attr {
                                isGrayStyle(true) // 灰色菊花
                                transform(Scale(1.2f, 1.2f))
                                marginRight(15f)
                            }
                        }
                    }
                }

                Text {
                    attr {
                        fontSize(14f)
                        color(Color(0xFFC5C5C5))
                        text(
                            when (ctx.footerRefreshState) {
                                FooterRefreshState.IDLE -> ""
                                FooterRefreshState.REFRESHING -> "正在加载..."
                                FooterRefreshState.NONE_MORE_DATA -> "没有更多了"
                                FooterRefreshState.FAILURE -> "加载失败，请点击重试"
                            }
                        )
                    }
                }
            }
        }
    }

}


internal class LoadMoreItemViewAttr : ComposeAttr()

internal class LoadMoreItemViewEvent : ComposeEvent() {
    var onLoadMore: (() -> Unit)? = null
}

internal fun ViewContainer<*, *>.LoadMoreItemView(init: InternalLoadMoreItemView.() -> Unit) {
    addChild(InternalLoadMoreItemView(), init)
}